# 导包
import time
import pytest
from util_tools.connectMysql import ConnectMysql
from util_tools.dingRebot import send_dd_msg
from util_tools.logs_util.recordlog import logs
from config.setting import is_dd_msg
import os

@pytest.fixture(scope='session', autouse=True)
def data_cleaning():
    conn = ConnectMysql()
    yield
    logs.info('正在清理测试数据...')
    sql = 'delete from ecs_users where user_name="test06"'
    conn.delete(sql)

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    total = terminalreporter._numcollected
    passed = len(terminalreporter.stats.get('passed', []))
    failed = len(terminalreporter.stats.get('failed', []))
    error = len(terminalreporter.stats.get('error', []))
    skipped = len(terminalreporter.stats.get('skipped', []))
    duration = round(time.time() - terminalreporter._sessionstarttime, 2)

    summary = f"""
    自动化测试结果通知，具体执行结果如下：
    测试用例总合计数：{total}
    测试执行通过数：{passed}
    测试执行失败数：{failed}
    错误数量：{error}
    跳过执行测试用例数量：{skipped}
    执行测试用例总耗时：{duration}秒
    测试项目名称：{total}
    测试项目构建编号：#{total}
    [构建地址链接]：({total})
    [Allure测试报告链接]：({total})
    """
    print(summary)
    if is_dd_msg:
        send_dd_msg(summary)